package cn.gcjxsc.repository.specs;

import cn.gcjxsc.consts.CheckStatus;
import cn.gcjxsc.consts.IntegralRecordType;
import cn.gcjxsc.data.query.salesman.ApplyInfoQuery;
import cn.gcjxsc.data.query.salesman.IntegralRecordQuery;
import cn.gcjxsc.data.query.salesman.SalesmanQuery;
import cn.gcjxsc.entity.salesman.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pumbaa
 * @since 1.0
 */
public class SalesmanSpec {
    /**
     * 查询申请列表
     *
     * @param filter 过滤条件
     * @return
     */
    public static Specification<ApplyInfo> buildFilter(final ApplyInfoQuery filter) {
        return new Specification<ApplyInfo>() {
            @Override
            public Predicate toPredicate(Root<ApplyInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                if (filter.getStartTime() != null && filter.getStartTime() > 0) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get(ApplyInfo_.created), filter.getStartTime()));
                }
                if (filter.getEndTime() != null && filter.getEndTime() > 0) {
                    predicateList.add(cb.lt(root.get(ApplyInfo_.created), filter.getEndTime()));
                }
                if (filter.getStoreId() != null && filter.getStoreId() > 0) {
                    predicateList.add(cb.equal(root.get(ApplyInfo_.storeId), filter.getStoreId()));
                }
                if (filter.getMemberId() != null && filter.getMemberId() > 0) {
                    predicateList.add(cb.equal(root.get(ApplyInfo_.memberId), filter.getMemberId()));
                }
                if (filter.getStatus() != null && filter.getStatus() > -1) {
                    predicateList.add(cb.equal(root.get(ApplyInfo_.status), CheckStatus.fromCode(filter.getStatus())));
                }
                if (!StringUtils.isEmpty(filter.getMobile())) {
                    predicateList.add(cb.like(root.get(ApplyInfo_.mobile), filter.getMobile() + "%"));
                }
                if (!StringUtils.isEmpty(filter.getName())) {
                    predicateList.add(cb.like(root.get(ApplyInfo_.name), "%" + filter.getName() + "%"));
                }
                Predicate[] predicates = new Predicate[predicateList.size()];

                return cb.and(predicateList.toArray(predicates));
            }
        };
    }

    /**
     * 查询业务员列表
     *
     * @param filter 过滤条件
     * @return
     */
    public static Specification<Salesman> buildSalesmanFilter(final SalesmanQuery filter) {
        return new Specification<Salesman>() {
            @Override
            public Predicate toPredicate(Root<Salesman> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                if (filter.getStartTime() != null && filter.getStartTime() > 0) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get(Salesman_.created), filter.getStartTime()));
                }
                if (filter.getEndTime() != null && filter.getEndTime() > 0) {
                    predicateList.add(cb.lt(root.get(Salesman_.created), filter.getEndTime()));
                }
                if (filter.getStoreId() != null && filter.getStoreId() > 0) {
                    predicateList.add(cb.equal(root.get(Salesman_.storeId), filter.getStoreId()));
                }
                if (filter.getMemberId() != null && filter.getMemberId() > 0) {
                    predicateList.add(cb.equal(root.get(Salesman_.memberId), filter.getMemberId()));
                }
                predicateList.add(cb.equal(root.get(Salesman_.status), 1));
                if (!StringUtils.isEmpty(filter.getMobile())) {
                    predicateList.add(cb.like(root.get(Salesman_.mobile), filter.getMobile() + "%"));
                }
                if (!StringUtils.isEmpty(filter.getName())) {
                    predicateList.add(cb.like(root.get(Salesman_.name), "%" + filter.getName() + "%"));
                }
                Predicate[] predicates = new Predicate[predicateList.size()];

                return cb.and(predicateList.toArray(predicates));
            }
        };
    }

    public static Specification<IntegralRecord> buildSalesmanFilter(final IntegralRecordQuery filter) {
        return new Specification<IntegralRecord>() {
            @Override
            public Predicate toPredicate(Root<IntegralRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                if (filter.getStartTime() != null && filter.getStartTime() > 0) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get(IntegralRecord_.created), filter.getStartTime()));
                }
                if (filter.getEndTime() != null && filter.getEndTime() > 0) {
                    predicateList.add(cb.lt(root.get(IntegralRecord_.created), filter.getEndTime()));
                }
                if (filter.getRefMemberId() != null && filter.getRefMemberId() > 0) {
                    predicateList.add(cb.equal(root.get(IntegralRecord_.memberId), filter.getRefMemberId()));
                }
                if (filter.getStoreId() != null && filter.getStoreId() > 0) {
                    predicateList.add(cb.equal(root.get(IntegralRecord_.storeId), filter.getStoreId()));
                }
                if (filter.getType() != null && filter.getType() > 0) {
                    predicateList.add(cb.equal(root.get(IntegralRecord_.type), IntegralRecordType.fromCode(filter.getType())));
                }
                if (filter.getSalesmanId() != null && filter.getSalesmanId() > 0) {
                    predicateList.add(cb.equal(root.get(IntegralRecord_.salesman), filter.getSalesmanId()));
                }
                if (!StringUtils.isEmpty(filter.getSourceId())) {
                    predicateList.add(cb.equal(root.get(IntegralRecord_.source), filter.getSourceId()));
                }

                Predicate[] predicates = new Predicate[predicateList.size()];

                return cb.and(predicateList.toArray(predicates));
            }
        };
    }

    public static Specification<RecommendMember> buildRecommendMemberFilter(final Integer memberId, final Integer storeId) {
        return new Specification<RecommendMember>() {
            @Override
            public Predicate toPredicate(Root<RecommendMember> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(cb.equal(root.get(RecommendMember_.memberId), memberId));
                predicateList.add(cb.equal(root.get(RecommendMember_.storeId), storeId));
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
